package com.ossjk.qlh.minapp.OrderID;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class OrderUtil {

    public static String generateKey(OrderType orderType) {
        StringBuilder stringBuilder = new StringBuilder(orderType.getCacheKeyPrefix());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(orderType.getDatePattern());
        stringBuilder.append(simpleDateFormat.format(new Date()));
        return stringBuilder.toString();
    }


    public static String generateNum(int serial, OrderType orderType) {
        //前缀+日期+随机码+流水号
        StringBuilder stringBuilder = new StringBuilder(orderType.getPrefix());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(orderType.getDatePattern());
        stringBuilder.append(simpleDateFormat.format(new Date()));
        stringBuilder.append(completionRandom(orderType));
        stringBuilder.append(completionSerial(serial,orderType));
        return stringBuilder.toString();
    }

    /**
     * 补全随机数
     *
     * @param orderType 单号生成枚举
     */
    public static String completionRandom(OrderType orderType) {
        StringBuffer sb = new StringBuffer();
        //随机数长度
        int length = orderType.getRandomLength();
        if (length > 0) {
            Random random = new Random();
            for (int i = 0; i < length; i++) {
                //十以内随机数补全
                sb.append(random.nextInt(10));
            }
        }
        return sb.toString();
    }


    /**
     * 补全流水号
     */
    public static String completionSerial(int serial, OrderType orderType) {
        return String.format("%0" + orderType.getSerialLength() + "d", serial);
    }

    public static void main(String[] args) {
        System.out.println(   OrderUtil.generateKey(OrderType.DD_ORDER));
        System.out.println(   OrderUtil.generateNum(2,OrderType.DD_ORDER));
    }
}
